<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\View\Engines;

use Lg\View\Engine;

/**
 * Description of PhpEngine
 *
 * @author Allen <allen@lg4.cn>
 */
class PhpEngine implements Engine {

    public $debug = false;

    public function render($path, $data = array(), $outputReturn = false) {
        $obLevel = ob_get_level();
        $error_level = error_reporting();
        if (!$this->debug) {
            error_reporting(0);
        }
        ob_start();
        extract($data, EXTR_SKIP);
        try {
            include $path;
        } catch (Exception $e) {
            $this->handleViewException($e, $obLevel);
        }
        error_reporting($error_level);
        if ($outputReturn) {
            return ltrim(ob_get_clean());
        }
        echo ltrim(ob_get_clean());
    }

    protected function handleViewException(Exception $e, $obLevel) {
        while (ob_get_level() > $obLevel) {
            ob_end_clean();
        }
        throw $e;
    }

}
